package cn.zktr.Personnel.model.service.tantiantian;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zktr.Personnel.model.dao.tantiantian.SlyPjtDao;
import cn.zktr.Personnel.model.dao.tantiantian.SlySetDao;
import cn.zktr.Personnel.model.dao.tantiantian.SlyTsxDao;
import cn.zktr.Personnel.model.dao.tantiantian.SlyYxzDao;
import cn.zktr.Personnel.model.mappers.tantiantian.PmsStmMapper;
import cn.zktr.Personnel.model.mappers.tantiantian.SlMapper;
import cn.zktr.Personnel.model.mappers.tantiantian.SlySetMapper;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerPmsHCont;
import cn.zktr.Personnel.model.pojos.PerPmsPot;
import cn.zktr.Personnel.model.pojos.PerSlyPjt;
import cn.zktr.Personnel.model.pojos.PerSlySet;
import cn.zktr.Personnel.model.pojos.PerSlyTsx;
import cn.zktr.Personnel.model.pojos.PerSlyYxz;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
//import cn.zktr.Personnel.model.service.zhouyongcong.monitService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveService;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitObj;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitgetService;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitsaveService;


@Service
@Transactional(rollbackOn=Exception.class)
public class SlySetService {
	@Autowired
	private findService fs;
	@Autowired
	private saveService ss;
	@Autowired
	private monitsaveService mss; 
	@Autowired
	private monitgetService mgs;
	/**
	 * 薪资套账Dao
	 */
	@Autowired
	private SlySetDao slyDao;
	
	@Autowired 
	private SlySetMapper setmer;
	
	@Autowired 
	private SlMapper slmer;
	
	/**
	 * 套账中间表
	 */
	@Autowired
	private SlyYxzDao yxzDao;
	/**
	 * 职员表
	 */
	@Autowired
	private PmsStmMapper stmmer;
	
	/**
	 * 套帐项目中间表
	 */
	@Autowired
	private SlyTsxDao txDao;
	
	/**
	 * 套帐项目表
	 */
	@Autowired
	private SlyPjtDao pjtDao;
	/**
	 * 员工
	 */
	@Autowired 
	private findService findser;
	
	
	/**
	 * @return查询所有套账
	 */
	
	public JSONArray selectSlyAll() {
		return slmer.select_set_();
	}
	public JSONArray selectSlyAlls() {
		return setmer.selectSets();
	}
	public void upSlySet(PerSlySet set) {
		slyDao.save(set);
	}
	/**
	 * @param slyset
	 * @return 新增薪资套账
	 */
	public void AddSlySet(
			String 	setname,String setrem,
			int perPmsPot
			,int perPmsEmp2
			,HttpSession session){
		PerSlySet set=new PerSlySet();
		set.setSetname(setname);
		set.setSetrem(setrem);
		//循环薪资套帐的人员id
		//职位
		set.setPerPmsPot(selepot(perPmsPot).get(0));
		//创建        
		set.setPerPmsEmp1(findser.findEmpid(Integer.valueOf(session.getAttribute("id")+"")));
		//审批
		set.setPerPmsEmp2(seleEmpid(perPmsEmp2).get(0));
		//状态
		set.setSetstat(0);
		//日期
		set.setSettime(new Timestamp(System.currentTimeMillis()));
		slyDao.save(set);
	}
	//新增套账项目中间表
	public void addtsx(Integer id,String text,PerSlySet set) {
			
				PerSlyPjt  selePjt= selePjt(id);
				PerSlyTsx tx=new PerSlyTsx();
				tx.setTsxval(text);
				tx.setPerSlyPjt(selePjt);
				tx.setPerSlySet(setmer.selesetlimit());
				txDao.save(tx);
	}
	//新增套账人员
	public void addyxz(Integer empids) {
		PerSlySet SlySet = setmer.selesetlimit();
		//根据员工id查询套帐-员工表，看是否原来有过套帐
		List<PerSlyYxz> list_yxz=null;
		list_yxz= yxzDao.select_yxz(empids);
		if(list_yxz.size()>0) {
			list_yxz.get(0).setPerSlySet(SlySet);
			 yxzDao.save(list_yxz.get(0));
		}else if(list_yxz.size()==0) {
			PerSlyYxz yxz=new PerSlyYxz();
			yxz.setPerSlySet(SlySet);
			yxz.setPerPmsEmp(seleEmpids(empids));
			yxzDao.save(yxz);
		}
			
	

	}
	//获取套账最新一条记录
	public PerSlySet selesetlimit() {
		return setmer.selesetlimit();
	}
	/**
	 * //根据职务分组，查询所有职务
	 */
	public JSONArray selePotZj() {
		return setmer.sele_zhiji();
	}
	/**
	 * @param potzj
	 * @return根据职位查询员工
	 */
	public JSONArray seleStmAll(Integer zhijiid) {
		return stmmer.seleStmAll(zhijiid);
	}
	/**
	 * @return查询薪资上级
	 */
	public JSONArray seleStmpotid(HttpSession session){
		JSONArray pot = stmmer.seleStmpotid();
	    if(pot.size()>0) {
			JSONObject o=pot.getJSONObject(0);
			if(o.get("EMPID").toString().equals(session.getAttribute("id")+"")) {
				return stmmer.seleStmpotid_zong();
				
			}else {
				return stmmer.seleStmpotid();

			}
		}
		
		return null;
		
	}
	/**
	 * @param empid
	 * @return 根据员工id查询员工信息
	 */
	public List<PerPmsEmp> seleEmpid(@Param("empid")Integer empid){
		return stmmer.seleEmpid(empid);
	}
	/**
	 * @return查找职级
	 */
	public List<PerPmsPot> selepot(Integer zhijiid){
		return stmmer.selepot(zhijiid);
	}
	
	/**
	 * @param tx新增套帐项目中间表
	 */
/*	public void addTx(PerSlyTsx tx) {
	
	}*/
	
	/**
	 * @param tx根据id查询套帐项目
	 * @return 
	 */
	public PerSlyPjt selePjt(Integer pjt) {
		return pjtDao.findById(pjt).get();
	}
	/**
	 * @param pjt
	 * @return新增薪资项目
	 */
	public PerSlyPjt AddPjt(PerSlyPjt pjt) {
		PerSlyPjt selectpjts_name = null;
		selectpjts_name=setmer.selectpjts_name(pjt.getPjtname());
		if(selectpjts_name!=null) {
			return null;
		}else{
			return pjtDao.save(pjt);
		}
	}
	/**
	 * @param empid
	 * @return  根据员工id查询单挑员工信息
	 */
	public PerPmsEmp seleEmpids(Integer empid){
		return stmmer.seleEmpids(empid);
		
	}
	/**
	 * @param setid
	 * @return根据薪资id查询改薪资淘掌柜下面员工姓名
	 */
	public JSONArray seleSetBySetId(Integer setid) {
		return setmer.seleSetBySetId(setid);
		
	}
	/**
	 * @param setid
	 * @return 根据薪资id查询改薪资淘掌柜下面项目名称
	 */
	public JSONArray seleBySetId_Tx(Integer setid) {
		return setmer.seleBySetId_Tx(setid);
		
	}
	
	/**
	 * @param setname
	 * @return 模糊查询，根据套账名称
	 */
	public JSONArray seleBySetName(String setname) {
		return setmer.seleBySetName(setname);
	}
	//根据员工ID查询套账
	public Map<String, Object> selectYuangongtaozhang(Integer empid) {
		Map<String,Object> map = new HashMap<>();
		JSONArray findAll=setmer.seleyuangongtaozhang(empid);
		System.err.println(findAll.size());
		//页内容
		map.put("rows",findAll);
		return map;
		
	}
	
	public monitObj setmo1(Integer setid) {
		PerSlySet set1 = slyDao.findById(setid).get();
		mgs.getPojos(set1);
		monitObj mo1 = new monitObj(false,false);
		set1.setSetstat(1);
		slyDao.save(set1);
		mo1.setOld_obj(set1);
		
		PerSlySet set2 = new PerSlySet();
		set2.setSetstat(2);
		mo1.setNew_obj(set2); 
		
		return mo1;
	}
	public void songshen(Integer setid,HttpSession session) {
		PerSlySet set = slyDao.findById(setid).get();
		monitObj[] mos = {setmo1(setid)};
		//1.审核类型
		//2.审核发起人
		//3.审核标题
		//4.审核的文本
		ss.save_Audit_id(5,set.getPerPmsEmp1().getEmpid(),"薪资套账审核", fs.findEmpid(set.getPerPmsEmp1().getEmpid()).getEmpname()+"发起薪资审核，请审核！");
		mss.save(findser.findEmpid(Integer.valueOf(session.getAttribute("id")+"")).getEmpid(), mos, 5);
		
		//mss.save_05(,set);
	}
	//员工调动
	public void yuagongdiaodong(Integer empid,Integer potid) {
		PerSlySet set = setmer.sele_pot_potid(potid);
		PerSlyYxz yxz = setmer.sele_YXZ_empid(empid);
		PerPmsEmp emp = stmmer.seleEmpids(empid);
		yxz.setPerSlySet(set);
		yxz.setPerPmsEmp(emp);
		yxzDao.save(yxz);
	}
	public void jinyong(Integer setid) {
		PerSlySet set = slyDao.findById(setid).get();
		set.setSetstat(-1);
		set.setSettime(null);
		slyDao.save(set);
		
	}
	public void qiyong(Integer setid) {
		PerSlySet set = slyDao.findById(setid).get();
		set.setSetstat(0);
		set.setSettime(new Timestamp(System.currentTimeMillis()));
		slyDao.save(set);
	}
	public PerPmsHCont sele_jibengognzi(Integer zhijiid) {
		// TODO Auto-generated method stub
		return stmmer.sele_jibengognzi(zhijiid);
	}
}
